﻿// SystemSetView.cpp: 实现文件
//

#include "stdafx.h"
#include "DC-QC.h"
#include "SystemSetView.h"
#include "CFlowCaliDlg.h"
#include "CGraphSetDlg.h"
#include "CFidParDlg.h"
#include "CWitsCfgDlg.h"
// CSystemSetView

IMPLEMENT_DYNCREATE(CSystemSetView, CFormView)

CSystemSetView::CSystemSetView()
	: CFormView(IDD_CSystemSetView)
	, m_bIsKioskPad(true)
{
	m_nCurID = -1;
	m_nOldMethod = -1;
}

CSystemSetView::~CSystemSetView()
{
}

void CSystemSetView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_BTFIRZF, m_btFirZF);
	DDX_Control(pDX, IDC_BTFIRQT, m_btFirQT);
	DDX_Control(pDX, IDC_BTSWITCH1, m_btSwitch1);
	DDX_Control(pDX, IDC_BTSWITCH2, m_btSwitch2);
	DDX_Radio(pDX, IDC_INTRADIO1, m_nIntType);
	DDX_Radio(pDX, IDC_CYRADIO1, m_nCycleType);
	DDX_Radio(pDX, IDC_MODERADIO1, m_nWorkType);
	DDX_Radio(pDX, IDC_RADIO7, m_nUnitType);
	DDX_Text(pDX, IDC_ZYXWD, m_dTempZW);
	DDX_Text(pDX, IDC_ZFWD, m_dTempZF_A);
	DDX_Text(pDX, IDC_ZFWD_B, m_dTempZF_B);
	DDX_Text(pDX, IDC_QTWD, m_dTempQT);
	DDX_Text(pDX, IDC_CH1AIR, m_dZF1Air);
	DDX_Text(pDX, IDC_CH1PRE, m_dZF1Pre);
	DDX_Text(pDX, IDC_CH1MAS, m_dZF1Mas);
	DDX_Text(pDX, IDC_CH2AIR, m_dZF2Air);
	DDX_Text(pDX, IDC_CH2PRE, m_dZF2Pre);
	DDX_Text(pDX, IDC_CH2MAS, m_dZF2Mas);
	DDX_Text(pDX, IDC_QTAIR, m_dQTAir);
	DDX_Text(pDX, IDC_QTH2, m_dQTH2);
	DDX_Text(pDX, IDC_QTSAMPLE, m_dQTSample);
	DDX_Text(pDX, IDC_WN, m_strWN);
	DDX_Text(pDX, IDC_PRECUTTIME, m_dPreCutTime);
	DDX_Control(pDX, IDC_BTKEYBOARD, m_btKeyboard);
}

BEGIN_MESSAGE_MAP(CSystemSetView, CFormView)
	ON_WM_CTLCOLOR()
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_BUTTON6, &CSystemSetView::OnBnClickedButton6)
	ON_BN_CLICKED(IDC_BUTTON7, &CSystemSetView::OnBnClickedButton7)
	ON_BN_CLICKED(IDC_BTAPPLY, &CSystemSetView::OnBnClickedBtapply)
	ON_BN_CLICKED(IDC_BTFLOWCALI, &CSystemSetView::OnBnClickedBtflowcali)
	ON_BN_CLICKED(IDC_BTFIRZF, &CSystemSetView::OnBnClickedBtfirzf)
	ON_BN_CLICKED(IDC_BTFIRQT, &CSystemSetView::OnBnClickedBtfirqt)
	ON_BN_CLICKED(IDC_BTSWITCH1, &CSystemSetView::OnBnClickedBtswitch1)
	ON_BN_CLICKED(IDC_BTSWITCH2, &CSystemSetView::OnBnClickedBtswitch2)
	ON_BN_CLICKED(IDC_BUTTON8, &CSystemSetView::OnBnClickedButton8)
	ON_BN_CLICKED(IDC_BTKEYBOARD, &CSystemSetView::OnBnClickedBtkeyboard)
	ON_BN_CLICKED(IDC_MODIFY, &CSystemSetView::OnBnClickedModify)
	ON_BN_CLICKED(IDC_MODERADIO1, &CSystemSetView::OnBnClickedModeradio1)
	ON_BN_CLICKED(IDC_MODERADIO2, &CSystemSetView::OnBnClickedModeradio2)
	ON_BN_CLICKED(IDC_MODERADIO3, &CSystemSetView::OnBnClickedModeradio3)
	ON_BN_CLICKED(IDC_MODERADIO4, &CSystemSetView::OnBnClickedModeradio4)
	ON_BN_CLICKED(IDC_CHECK1, &CSystemSetView::OnBnClickedCheck1)
	ON_BN_CLICKED(IDC_CHECK2, &CSystemSetView::OnBnClickedCheck2)
	ON_BN_CLICKED(IDC_BTFIRZF_B, &CSystemSetView::OnBnClickedBtfirzfB)
	ON_EN_SETFOCUS(IDC_CH1AIR, &CSystemSetView::OnSetFocus)  
	ON_EN_SETFOCUS(IDC_CH2AIR, &CSystemSetView::OnSetFocus)  
	ON_EN_SETFOCUS(IDC_QTAIR, &CSystemSetView::OnSetFocus)  
	ON_EN_SETFOCUS(IDC_CH1PRE, &CSystemSetView::OnSetFocus)  
	ON_EN_SETFOCUS(IDC_CH2PRE, &CSystemSetView::OnSetFocus)  
	ON_EN_SETFOCUS(IDC_QTH2, &CSystemSetView::OnSetFocus)  
	ON_EN_SETFOCUS(IDC_CH1MAS, &CSystemSetView::OnSetFocus)  
	ON_EN_SETFOCUS(IDC_CH2MAS, &CSystemSetView::OnSetFocus)  
	ON_EN_SETFOCUS(IDC_QTSAMPLE, &CSystemSetView::OnSetFocus)  

	ON_EN_SETFOCUS(IDC_PRECUTTIME, &CSystemSetView::OnSetFocus)  
	ON_EN_SETFOCUS(IDC_ZYXWD, &CSystemSetView::OnSetFocus)  
	ON_EN_SETFOCUS(IDC_QTWD, &CSystemSetView::OnSetFocus)  
	ON_EN_SETFOCUS(IDC_ZFWD, &CSystemSetView::OnSetFocus)  
	ON_EN_SETFOCUS(IDC_ZFWD_B, &CSystemSetView::OnSetFocus)  

	ON_EN_SETFOCUS(IDC_WN, &CSystemSetView::OnSetFocusH)  
END_MESSAGE_MAP()


// CSystemSetView 诊断

#ifdef _DEBUG
void CSystemSetView::AssertValid() const
{
	CFormView::AssertValid();
}

#ifndef _WIN32_WCE
void CSystemSetView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif
#endif //_DEBUG


// CSystemSetView 消息处理程序


BOOL CSystemSetView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
	// TODO: 在此添加专用代码和/或调用基类

	return CFormView::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}


HBRUSH CSystemSetView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);

	// TODO:  在此更改 DC 的任何特性
	if (nCtlColor != CTLCOLOR_EDIT)
	{
		switch (pWnd->GetDlgCtrlID())
		{
		case IDC_G1:
		case IDC_G2:
		case IDC_G3:
		case IDC_G4:
			pDC->SetTextColor(RGB(255, 0, 0));
			pDC->SetBkMode(TRANSPARENT);
			break;
		case IDC_MACHINEINFO:
			pDC->SetTextColor(RGB(255, 255, 128));
			pDC->SetBkMode(TRANSPARENT);
			return gl.m_brBack2;
			break;
		default:
			pDC->SetBkMode(TRANSPARENT);
			break;
		}
		return gl.m_brBack1;
	}
	if (nCtlColor == CTLCOLOR_EDIT)
	{
		switch (pWnd->GetDlgCtrlID())
		{
		case IDC_ZYXWD:
		case IDC_ZFWD:
		case IDC_QTWD:
		case IDC_CH1AIR:
		case IDC_CH1PRE:
		case IDC_CH1MAS:
		case IDC_CH2AIR:
		case IDC_CH2PRE:
		case IDC_CH2MAS:
		case IDC_QTAIR:
		case IDC_QTH2:
		case IDC_WN:
			pDC->SetTextColor(RGB(0, 0, 0));
			pDC->SetBkMode(0);
			break;
		default:
			pDC->SetBkMode(TRANSPARENT);
			break;
		}
	}
	// TODO:  如果默认的不是所需画笔，则返回另一个画笔
	return hbr;
}


void CSystemSetView::OnPaint()
{
	CPaintDC dc(this); // device context for painting
					   // TODO: 在此处添加消息处理程序代码
					   // 不为绘图消息调用 CFormView::OnPaint()
	CRect rect;
	GetClientRect(&rect);

	CBitmap ClientScreen, *pOldBitmap;
	CRect UpdateRect(rect);
	CDC MDC;
	MDC.CreateCompatibleDC(&dc);
	ClientScreen.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());
	pOldBitmap = MDC.SelectObject(&ClientScreen);
	MDC.FillSolidRect(UpdateRect, RGB(255, 255, 255));
	Draw(&MDC, rect);
	dc.BitBlt(UpdateRect.left, UpdateRect.top,
		UpdateRect.right - UpdateRect.left,
		UpdateRect.bottom - UpdateRect.top,
		&MDC, UpdateRect.left, UpdateRect.top, SRCCOPY);
	MDC.SelectObject(pOldBitmap);

	ClientScreen.DeleteObject();
	MDC.DeleteDC();
}

void	CSystemSetView::Draw(CDC* pDC, CRect rt)
{
	try
	{
		DrawInBorder(pDC, rt);
		rt.DeflateRect(1, 1, 1, 1);
		pDC->FillSolidRect(rt, BACKCOLOR2);
	}
	catch (...)
	{
	}
}

void CSystemSetView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();

	// TODO: 在此添加专用代码和/或调用基类
	m_nOldMethod=m_nIntType = gl.gcMachine.nIntMethod;
	m_nCycleType = gl.m_nCycleType;
	m_nWorkType = gl.m_nWorkMode;
	m_nUnitType = gl.m_nUnit;
	m_dTempZW = gl.gcMachine.dColumTemp;
	m_dTempZF_A = gl.gcMachine.dFid1Temp;
	m_dTempZF_B = gl.gcMachine.dFid2Temp;
	m_dTempQT = gl.gcMachine.dFid3Temp;
	m_dZF1Air = gl.gcMachine.dAAirPSet;
	m_dZF1Pre = gl.gcMachine.dAH21PSet;
	m_dZF1Mas = gl.gcMachine.dAH22PSet;
	m_dZF2Air = gl.gcMachine.dBAirPSet;
	m_dZF2Pre = gl.gcMachine.dBH21PSet;
	m_dZF2Mas = gl.gcMachine.dBH22PSet;
	m_dQTAir = gl.gcMachine.dCAirPSet;
	m_dQTH2 = gl.gcMachine.dCH2PSet;
	m_dQTSample=gl.gcMachine.dCSamplePSet;
	m_strWN = gl.m_strWellName;
	m_dPreCutTime = gl.gcMachine.dInjectTime;

	/*m_dOldTemp[0] = m_dTempZW;
	m_dOldTemp[1] = m_dTempZF_A;
	m_dOldTemp[2] = m_dTempQT;
	m_dOldPre[0] = m_dZF1Air;
	m_dOldPre[1] = m_dZF1Pre;
	m_dOldPre[2] = m_dZF1Mas;
	m_dOldPre[3] = m_dZF2Air;
	m_dOldPre[4] = m_dZF2Pre;
	m_dOldPre[5] = m_dZF2Mas;
	m_dOldPre[6] = m_dQTAir;
	m_dOldPre[7] = m_dQTH2;*/
	m_btKeyboard.SetIcon(IDI_KEYBOARD, NULL);

	if(gl.m_bAarmSwitchOff)
		((CButton*)GetDlgItem(IDC_CHECK1))->SetCheck(1);
	else
		((CButton*)GetDlgItem(IDC_CHECK1))->SetCheck(0);

	if(gl.m_bUseBaseLineAdj)
		((CButton*)GetDlgItem(IDC_CHECK2))->SetCheck(1);
	else
		((CButton*)GetDlgItem(IDC_CHECK2))->SetCheck(0);


	
	UpdateData(FALSE);
}

//色谱图形设置
void CSystemSetView::OnBnClickedButton6()
{
	// TODO: 在此添加控件通知处理程序代码
	CGraphSetDlg dlg;
	if (dlg.DoModal() == IDOK)
	{
		CString strFileName;
		strFileName.Format(_T("%s\\SystemMethod.mtd"), m_strAppPath);
		gl.SaveCalMethod(strFileName);
	}
}

//放大器灵敏度
void CSystemSetView::OnBnClickedButton7()
{
	// TODO: 在此添加控件通知处理程序代码
	CFidParDlg dlg;
	if (dlg.DoModal() == IDOK)
	{
		CString strFileName;
		strFileName.Format(_T("%s\\SystemMethod.mtd"), m_strAppPath);
		gl.SaveCalMethod(strFileName);
	}
}

//应用
void CSystemSetView::OnBnClickedBtapply()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	gl.gcMachine.nIntMethod=m_nIntType;
	gl.m_nCycleType = m_nCycleType;
	gl.m_nWorkMode = m_nWorkType;
	gl.m_nUnit = m_nUnitType;
	gl.gcMachine.dColumTemp = m_dTempZW;
	gl.gcMachine.dFid1Temp = m_dTempZF_A;
	gl.gcMachine.dFid2Temp = m_dTempZF_B;
	gl.gcMachine.dFid3Temp = m_dTempQT;
	gl.gcMachine.dAAirPSet = m_dZF1Air;
	gl.gcMachine.dAH21PSet = m_dZF1Pre;
	gl.gcMachine.dAH22PSet = m_dZF1Mas;
	gl.gcMachine.dBAirPSet = m_dZF2Air;
	gl.gcMachine.dBH21PSet = m_dZF2Pre;
	gl.gcMachine.dBH22PSet = m_dZF2Mas;
	gl.gcMachine.dCAirPSet = m_dQTAir;
	gl.gcMachine.dCH2PSet = m_dQTH2;
	gl.gcMachine.dCSamplePSet= m_dQTSample;
	gl.gcMachine.dInjectTime=m_dPreCutTime;


	if (m_nOldMethod != m_nIntType)
	{
		for (int i = 0; i < 10; i++)
		{
			for (int k = 0; k < 10; k++)
			{
				if(gl.gcMachine.nIntMethod ==0)
				{
					gl.gcCi.scA[i].Area[k]= gl.gcCi.scA[i].OrgArea[k];
					gl.gcCi.scB[i].Area[k]= gl.gcCi.scB[i].OrgArea[k];
				}
				else
				{
					gl.gcCi.scA[i].Area[k] = gl.gcCi.scA[i].OrgHeight[k];
					gl.gcCi.scB[i].Area[k] = gl.gcCi.scB[i].OrgHeight[k];
				}
			}
		}
		m_nOldMethod = m_nIntType;
	}

	CString strFileName;
	strFileName.Format(_T("%s\\SystemMethod.mtd"), m_strAppPath);
	gl.SaveCalMethod(strFileName);

	gl.SaveSoftwareSetting();

	if (gl.m_nWorkMode == 3)
		cControl.OpenRelay3(FALSE);
	else
		cControl.OpenRelay3(TRUE);

	if (gl.m_nCycleType == 1)	//工作周期，0--30秒，1--90秒；
		gl.gcMachine.dStabilizeTime = 60;
	else
		gl.gcMachine.dStabilizeTime = 0;

	//if (m_dOldTemp[0] != m_dTempZW || m_dOldTemp[1] != m_dTempZF_A || m_dOldTemp[2] != m_dTempQT)
	cControl.SetTempPar();

	/*bool bNeedSetPre = FALSE;
	if(m_dOldPre[0] != m_dZF1Air || m_dOldPre[1] != m_dZF1Pre || m_dOldPre[2] != m_dZF1Mas || m_dOldPre[6] != m_dQTAir)
		bNeedSetPre = TRUE;
	if(m_dOldPre[3] != m_dZF2Air || m_dOldPre[4] != m_dZF2Pre || m_dOldPre[5] != m_dZF2Mas || m_dOldPre[7] != m_dQTH2)
		bNeedSetPre = TRUE;
	if(bNeedSetPre)*/
	cControl.SetEPCPars();
	if (((CButton*)GetDlgItem(IDC_CHECK1))->GetCheck() == 1)
		gl.m_bAarmSwitchOff=TRUE;
	else
		gl.m_bAarmSwitchOff=FALSE;

	if (((CButton*)GetDlgItem(IDC_CHECK2))->GetCheck() == 1)
		gl.m_bUseBaseLineAdj=TRUE;
	else
		gl.m_bUseBaseLineAdj=FALSE;
	
}

void	CSystemSetView::RefreshView()
{
	CString strTemp;
	strTemp.Format(_T("仪器固件信息 （硬件序列号：%d，出厂日期：%d，固件版本号：%d）"), gl.gcStatus.m_nMachineSN, gl.gcStatus.m_nProduceDate, gl.gcStatus.m_nFirmwareVer);
	GetDlgItem(IDC_MACHINEINFO)->SetWindowTextW(strTemp);
}

//流量标定
void CSystemSetView::OnBnClickedBtflowcali()
{
	// TODO: 在此添加控件通知处理程序代码
	CFlowCaliDlg dlg;
	dlg.DoModal();
}

//组份点火
void CSystemSetView::OnBnClickedBtfirzf()
{
	// TODO: 在此添加控件通知处理程序代码
	cControl.Fire(1);

}

//全烃点火
void CSystemSetView::OnBnClickedBtfirqt()
{
	// TODO: 在此添加控件通知处理程序代码
	cControl.Fire(0);

}

//组份A 切换
void CSystemSetView::OnBnClickedBtswitch1()
{
	// TODO: 在此添加控件通知处理程序代码
	cControl.SendSwitchCmd(0);
}

//组份B 切换
void CSystemSetView::OnBnClickedBtswitch2()
{
	// TODO: 在此添加控件通知处理程序代码
	cControl.SendSwitchCmd(1);
}

//WITS配置
void CSystemSetView::OnBnClickedButton8()
{
	// TODO: 在此添加控件通知处理程序代码
	CWitsCfgDlg dlg;
	dlg.DoModal();
}


void CSystemSetView::OnBnClickedBtkeyboard()
{
    // 切换键盘类型
	gl.ButtonToggle();
}
//修改井名
void CSystemSetView::OnBnClickedModify()
{
	// TODO: 在此添加控件通知处理程序代码
	CString strFileName,strSourceFile;
	GetDlgItem(IDC_WN)->GetWindowTextW(m_strWN);
	m_strWN.Trim();
	if (m_strWN != gl.m_strWellName)
	{
		//创建新井的数据库文件
		strFileName.Format(_T("%s\\Wells\\%s.mdb"), m_strAppPath,m_strWN);
		strSourceFile.Format(_T("%s\\QC_System.mdb"), m_strAppPath);
		if (FileExist(strFileName))
		{
			if (AfxMessageBox(_T("此井名的色谱数据库文件已存在，是否覆盖？"), MB_YESNO | MB_ICONSTOP) == IDYES)
				CopyFile(strSourceFile, strFileName, FALSE);
		}
		gl.m_strWellName = m_strWN;
		gl.OpenCurWellDB();
		gl.SaveSoftwareSetting();
	}
}

void	CSystemSetView::UpateWorkMode()
{
	UpdateData(TRUE);
	gl.m_nWorkMode = m_nWorkType;
	cControl.SetEPCPars();
}


void CSystemSetView::OnBnClickedModeradio1()
{
	// TODO: 在此添加控件通知处理程序代码
	UpateWorkMode();
}


void CSystemSetView::OnBnClickedModeradio2()
{
	// TODO: 在此添加控件通知处理程序代码
	UpateWorkMode();
}


void CSystemSetView::OnBnClickedModeradio3()
{
	// TODO: 在此添加控件通知处理程序代码
	UpateWorkMode();
}


void CSystemSetView::OnBnClickedModeradio4()
{
	// TODO: 在此添加控件通知处理程序代码
	UpateWorkMode();
}

//报警总开关
void CSystemSetView::OnBnClickedCheck1()
{
	// TODO: 在此添加控件通知处理程序代码
	if (((CButton*)GetDlgItem(IDC_CHECK1))->GetCheck() == 1)
		gl.m_bAarmSwitchOff=TRUE;
	else
		gl.m_bAarmSwitchOff=FALSE;
}


void CSystemSetView::OnBnClickedCheck2()
{
	// TODO: 在此添加控件通知处理程序代码
	if (((CButton*)GetDlgItem(IDC_CHECK2))->GetCheck() == 1)
		gl.m_bUseBaseLineAdj=TRUE;
	else
		gl.m_bUseBaseLineAdj=FALSE;
}


void CSystemSetView::OnBnClickedBtfirzfB()
{
	// TODO: 在此添加控件通知处理程序代码
	cControl.Fire(2);
}

void CSystemSetView::OnSetFocus()
{
    //HWND hWnd = ::GetFocus(); // 获取当前焦点所在的窗口句柄
    //if (hWnd != nullptr)
    //{
    //    // 获取当前控件的 ID
    //    int nCtrlID = ::GetDlgCtrlID(hWnd);
    //    
    //    // 根据控件的 ID 来判断是哪个编辑框获得了焦点
    //    switch (nCtrlID)
    //    {
    //    case IDC_CH1AIR:
    //    case IDC_CH2AIR:
    //    case IDC_QTAIR:
    //    case IDC_CH1PRE:
    //    case IDC_CH2PRE:
    //    case IDC_QTH2:
    //    case IDC_CH1MAS:
    //    case IDC_CH2MAS:
    //    case IDC_QTSAMPLE:
    //    case IDC_PRECUTTIME:
    //    case IDC_ZYXWD:
    //    case IDC_QTWD:
    //    case IDC_ZFWD:
    //    case IDC_ZFWD_B:
    //        // 当这些控件获得焦点时，执行所需操作
    //        gl.ButtonToggleNum(); // 你的自定义操作
    //        break;

    //    default:
    //        // 如果没有匹配到的控件 ID，可选择不做任何操作
    //        break;
    //    }
    //}
}

void CSystemSetView::OnSetFocusH()
{
	// HWND hWnd = ::GetFocus(); // 获取当前焦点所在的窗口句柄
 //   if (hWnd != nullptr)
 //   {
 //       // 获取当前控件的 ID
 //       int nCtrlID = ::GetDlgCtrlID(hWnd);
	//	if(IDC_WN==nCtrlID){
	//		gl.ButtonToggleH();
	//	}
	//}
}
